<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 * 接口加密函数 ---公钥加密
 *@param mixed $data 要加密的数据
 *@param string $system 系统加密公钥
 */
function rsa_encrypt($system='',$data = ''){
    $encrypt_str = '';
    $encrypt_key = '';
    switch ($system) {
        case 'pub':
                $encrypt_key = 'pubpubkey';
            break;
        default :
                $encrypt_key = 'pubkey';
            break;
    }
    $public_key = config($encrypt_key);
    $encrypted = '';
    if(empty($data)) return $data;
    $pub_key = openssl_pkey_get_public($public_key);
    if(is_array($data)){
        $data = json_encode($data,JSON_UNESCAPED_UNICODE);
        $count = ceil(mb_strlen($data)/81);
        $j = 1;
        for ($i=$count; $i >0 ; $i--) {
            $a = mb_substr($data, ($j-1)*81,81);
            openssl_public_encrypt($a, $encrypted, $pub_key);//公钥加密
            $encrypted = base64_encode($encrypted);// base64传输
            $encrypt_str .= $encrypted.';';
            $j++;
        }
    }else{
        $count = ceil(mb_strlen($data)/81);
        $j = 1;
        for ($i=$count; $i >0 ; $i--) {
            $a = mb_substr($data, ($j-1)*81,81);
            openssl_public_encrypt($a, $encrypted, $pub_key);//公钥加密
            $encrypted = base64_encode($encrypted);// base64传输
            $encrypt_str .= $encrypted.';';
            $j++;
        }
    }
    #去除最后一个分号
    $encrypt_str = mb_substr($encrypt_str, 0,-1);
    $encrypt_str = rawurlencode($encrypt_str);
    return $encrypt_str;
}

/**
 * 接口数据解密----私钥解密
 * @param  string $data 要解密的数据
 */
function rsa_decrypt($data = ''){
    $private_key = config('prikey');
    $pri_key = openssl_get_privatekey($private_key);
    if(is_array($data)){
        foreach ($data as $key => &$value) {
            $result = '';
            if(in_array($key, ['id','username','password','phoneno','uniqid','refer_id','grade','realname','idcard','paypassword'])){
                $value = rawurldecode($value);
                if(empty($value)) continue;
                // return openssl_pkey_get_details($pri_key);
                $arr = explode(';', $value);
                foreach ($arr as $k1 => $v1) {
                    if(empty($v1)) continue;
                    $v1 = base64_decode($v1);
                    openssl_private_decrypt($v1, $decrypted, $pri_key);//私钥解密
                    $result .= $decrypted;
                }
                $value = $result;
            }
        }
        return $data;
    }else{
        $result = '';
        $data = rawurldecode($data);
        if(empty($data)) return $data;
        // return openssl_pkey_get_details($pri_key);
        $arr = explode(';', $data);
        foreach ($arr as $key => $value) {
            if(empty($value)) continue;
            $value = base64_decode($value);
            openssl_private_decrypt($value, $decrypted, $pri_key);//私钥解密
            $result .= $decrypted;
        }
    }
    
    return $result;
}
/**
 * 发送get请求
 * @param string $url
 * @return bool|mixed
 */
function request_get($url = '') {
    if (empty($url)) {
        return false;
    }

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
    //curl_redir_exec($ch);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $data = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Errno' . curl_error($ch);
    }
    curl_close($ch);
    return $data;
}

/**
 * 发送post请求
 * @param string $url
 * @param string $param
 * @return bool|mixed
 */
function request_post($url = '', $param = '') {
    if (empty($url) || empty($param)) {
        return false;
    }

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
//    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $data = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Errno' . curl_error($ch);
    }
    curl_close($ch);
    return $data;
}
function u_trim($string){
    $string = trim($string);
    $string = str_replace(' ', '', $string);    $string = str_replace(' ', '', $string);
    return $string;
}

/**
 * 用户密码加密方法
 * @param  string $str      加密的字符串
 * @param  [type] $auth_key 加密符
 * @return string           加密后长度为32的字符串
 */
function user_md5($str, $auth_key = '')
{
    return '' === $str ? '' : md5(sha1($str) . $auth_key);
}